import type { MockMethod } from 'vite-plugin-mock'
import { faker } from '@faker-js/faker'
import type { MenuResponse } from '@/services/modules';
export const mockStructure = {
    ok(data: Record<string, any>, message?: string) {
        return {
            code: 200,
            data,
            message,
        };
    },
    error(msg: string) {
        return {
            code: 500,
            message: msg,
        };
    },
};
// 模拟数据
let role = ''
const userInfo = {
    avatar: faker.image.avatar(),
    name: faker.person.fullName(),
    email: faker.internet.email(),
    phone: faker.phone.number(),
    // avatar: 'https://img2.baidu.com/it/u=3422452222,2320731328&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    // name: '张三',
    // email: 'zhangsan@example.com',
    // phone: '1234567890',
}

const menuList: MenuResponse[] = [
    {
        id: 1,
        name: 'main',
        path: '/main',
        pid: 0,
        redirect: '/main/board',
        meta: {
            title: '菜单',
            icon: 'icon-main',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 2,
        name: 'board',
        path: '/main/board',
        pid: 1,
        redirect: '',
        meta: {
            title: '控制台',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 3,
        name: 'role',
        path: '/main/role',
        pid: 1,
        redirect: '/main/role/addRole',
        meta: {
            title: '角色管理',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 31,
        name: 'addRole',
        path: '/main/role/addRole',
        pid: 3,
        meta: {
            title: '添加角色',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 32,
        name: 'roleList',
        path: '/main/role/list',
        pid: 3,
        meta: {
            title: '角色列表',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 20,
        name: 'main1',
        path: '/main1',
        pid: 0,
        redirect: '/main1/board',
        meta: {
            title: '菜单',
            icon: 'icon-main',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 21,
        name: 'board1',
        path: '/main1/board',
        pid: 20,
        redirect: '',
        meta: {
            title: '控制台',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 300,
        name: 'role1',
        path: '/main1/role',
        pid: 20,
        redirect: '/main1/role/addRole',
        meta: {
            title: '角色管理',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: true,
    },
    {
        id: 301,
        name: 'addRole1',
        path: '/main1/role/addRole',
        pid: 300,
        meta: {
            title: '添加角色',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: true,
    },
    {
        id: 4,
        name: 'parent',
        path: '/main/parent',
        pid: 1,
        redirect: '/main/parent/children1',
        meta: {
            title: '父菜单',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 41,
        name: 'children1',
        path: '/main/parent/children1',
        pid: 4,
        redirect: '',
        meta: {
            title: '子菜单1',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 40,
        name: 'children40',
        path: '',
        pid: 4,
        redirect: '/main/parent/children/children2',
        meta: {
            title: '子菜单0',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 401,
        name: 'children401',
        path: '/main/parent/children/children2',
        pid: 40,
        redirect: '',
        meta: {
            title: '子子菜单1',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 400,
        name: 'children400',
        path: '',
        pid: 40,
        redirect: '/main/parent/children/children/children3',
        meta: {
            title: '子子菜单0',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 4001,
        name: 'children4001',
        path: '/main/parent/children/children/children3',
        pid: 400,
        redirect: '',
        meta: {
            title: '子子子菜单0',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
    {
        id: 4000,
        name: 'children4000',
        path: '/main/parent/children/children/children4',
        pid: 400,
        redirect: '',
        meta: {
            title: '子子子菜单1',
            icon: 'icon-board',
        },
        alwaysShow: true,
        keepAlive: false,
    },
];
export default [
    // 登录接口
    {
        url: '/api/login',
        method: 'post',
        timeout: 500,
        response: ({ body }) => {
            const { username, password } = body as { username: string; password: string }

            if (username === 'normalization' && password === 'normalization8520') {
                role = 'admin'
                return mockStructure.ok(
                    {
                        token: 'admin',
                        userInfo: {
                            ...userInfo,
                            id: 1,
                            role,
                        },
                    },
                    '登录成功,欢迎回家!!!'
                )
            } else if (username === 'role1' && password === 'role123') {
                role = 'role1'
                return mockStructure.ok(
                    {
                        token: 'role1',
                        role,
                        userInfo: {
                            ...userInfo,
                            id: 2,
                            role,
                        },
                    },
                    '登录成功,欢迎回家!!!'
                )
            } else if (['normalization', 'role1'].includes(username)) {
                return mockStructure.error('用户名或密码错误')
            } else {
                return mockStructure.error('Invalid credentials')
            }
        },
    },

    // 获取用户信息
    {
        url: '/api/user/info',
        method: 'get',
        response: ({ headers }) => {
            // const token = headers.authorization
            return mockStructure.ok({
                userInfo: {
                    id: role === 'admin' ? 1 : 2,
                    role,
                    ...userInfo,
                },
            })
        },
    },

    // 获取菜单
    {
        url: '/api/menu',
        method: 'get',
        response: () => {
            return mockStructure.ok(menuList)
        },
    },
] as MockMethod[]